﻿using UnityEngine;
using System.Collections;

public class Champion : MonoBehaviour {

	public int hp, gold;
	GameObject hpDisplay, goldDisplay;
	GameObject minionSpawn;
	public GameObject projectile;

	public float attackRange;
	// Use this for initialization
	void Start () {
		hpDisplay = GameObject.Find (gameObject.name + "HP");
		minionSpawn = GameObject.Find ("MinionSpawn");
		projectile = (GameObject)Resources.Load ("Projectile");
	}
	
	// Update is called once per frame
	void Update () {
		if(hp <= 0){
			Destroy(gameObject);
		}	

		foreach(GameObject minion in minionSpawn.GetComponent<SpawnController>().minions){
			if (minion != null)
			{
				if(Distance(minion.transform.position, transform.position) <= attackRange){
					//Debug.Log ("Inrange");
					this.GetComponent<TowerController>().AddMinionTarget(minion);
				}
			}
		}

		hpDisplay.GetComponent<tk2dTextMesh>().text = hp.ToString();
	}
	
	float Distance(Vector3 p1, Vector3 p2){
		float dist = Mathf.Pow( (p2.x - p1.x), 2) + Mathf.Pow ( (p2.y - p1.y), 2);
		return Mathf.Sqrt(dist);
	}
}
